﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Agresso.Interface.Layer.Gateway;
using Agresso.Interface.Layer.Domain;
using System.Data.SqlClient;
using Agresso.Interface.Layer.SQL;
using System.Collections;

namespace Agresso.Interface.Layer.Service
{
    public class ResponsibleUserService
    {
        DataBaseConnectivity m_dataBaseConectivity;
        public ResponsibleUserService()
        {
            m_dataBaseConectivity = new DataBaseConnectivity();
        }
        public ResponsibleUser GetResponsibleUserByName(string name)
        {
            ResponsibleUser result = null;
            try
            {
                m_dataBaseConectivity.InterfaceSqlConnection.Open();
                SqlCommand cmd = new SqlCommand(ResponsibleUserSQL.FindBySoftmarName(), m_dataBaseConectivity.InterfaceSqlConnection);
                cmd.Parameters.Add("@softmar_user", System.Data.SqlDbType.NVarChar).Value = name.Trim();
                SqlDataReader rd = cmd.ExecuteReader();
                while (rd.Read())
                    result = new ResponsibleUser(rd);
                rd.Close();
                if (result == null)
                {
                    cmd.CommandText = ResponsibleUserSQL.Insert();
                    cmd.Parameters.Clear();
                    cmd.Parameters.Add("@softmar_user", System.Data.SqlDbType.NVarChar).Value = name.Trim();
                    cmd.Parameters.Add("@agresso_user", System.Data.SqlDbType.NVarChar).Value = "carina";
                    cmd.ExecuteNonQuery();

                    cmd.CommandText = ResponsibleUserSQL.GetMaxID();
                    cmd.Parameters.Clear();
                    result = new ResponsibleUser();
                    result.softmar_user = name;
                    result.ResponsibleUser_id = Convert.ToInt32(cmd.ExecuteScalar());
                }
            }
            catch (Exception x)
            {
                LogService.WriteLogException(x);
                throw x;
            }
            finally
            {
                m_dataBaseConectivity.InterfaceSqlConnection.Close();
            }
            return result;
        }
        public ResponsibleUser GetResponsibleUserByName(string name, string defaultuser)
        {
            ResponsibleUser result = null;
            try
            {
                m_dataBaseConectivity.InterfaceSqlConnection.Open();
                SqlCommand cmd = new SqlCommand(ResponsibleUserSQL.FindBySoftmarName(), m_dataBaseConectivity.InterfaceSqlConnection);
                cmd.Parameters.Add("@softmar_user", System.Data.SqlDbType.NVarChar).Value = name.Trim();
                SqlDataReader rd = cmd.ExecuteReader();
                while (rd.Read())
                    result = new ResponsibleUser(rd);
                rd.Close();
                if (result == null)
                {
                    cmd.CommandText = ResponsibleUserSQL.Insert();
                    cmd.Parameters.Clear();
                    cmd.Parameters.Add("@softmar_user", System.Data.SqlDbType.NVarChar).Value = name.Trim();
                    cmd.Parameters.Add("@agresso_user", System.Data.SqlDbType.NVarChar).Value = defaultuser.Trim();
                    cmd.ExecuteNonQuery();

                    cmd.CommandText = ResponsibleUserSQL.GetMaxID();
                    cmd.Parameters.Clear();
                    result = new ResponsibleUser();
                    result.softmar_user = name;
                    result.ResponsibleUser_id = Convert.ToInt32(cmd.ExecuteScalar());
                }
            }
            catch (Exception x)
            {
                LogService.WriteLogException(x);
                throw x;
            }
            finally
            {
                m_dataBaseConectivity.InterfaceSqlConnection.Close();
            }
            return result;
        }

        public IList GetAllResponsibleUser()
        {
            IList result = new ArrayList();
            try
            {
                m_dataBaseConectivity.InterfaceSqlConnection.Open();
                SqlCommand cmd = new SqlCommand(ResponsibleUserSQL.GetAllUser(), m_dataBaseConectivity.InterfaceSqlConnection);
                SqlDataReader rd = cmd.ExecuteReader();
                while (rd.Read())
                {
                    result.Add(new ResponsibleUser(rd));
                }
                rd.Close();
            }
            catch (Exception x)
            {
                LogService.WriteLogException(x);
                throw x;
            }
            finally
            {
                m_dataBaseConectivity.InterfaceSqlConnection.Close();
            }
            return result;
        }

        public void UpdateResponsibleUser(ResponsibleUser m_ResponsibleUser)
        {
            try
            {
                m_dataBaseConectivity.InterfaceSqlConnection.Open();
                SqlCommand cmd = new SqlCommand(ResponsibleUserSQL.UpdateResponsibleUser(), m_dataBaseConectivity.InterfaceSqlConnection);
                cmd.Parameters.Add("@agresso_user", System.Data.SqlDbType.NVarChar).Value = m_ResponsibleUser.agresso_user.Trim();
                cmd.Parameters.Add("@ResponsibleUser_id", System.Data.SqlDbType.Int).Value = m_ResponsibleUser.ResponsibleUser_id;
                cmd.ExecuteNonQuery();
            }
            catch (Exception x)
            {
                LogService.WriteLogException(x);
                throw x;
            }
            finally
            {
                m_dataBaseConectivity.InterfaceSqlConnection.Close();
            }
        }
    }
}
